package com.louis.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


/**
 * <p>
 * BeanController
 * </p>
 * 
 * @author: louisjiang
 * @version: 1.0.0,Apr 23, 20125:38:10 PM
 */
@Scope("request")
@Controller("beanController")
public class BeanController {

	@RequestMapping(value = "/beans/beans.html", method = RequestMethod.GET)
	public String gotoBeans() {
		return "beans";
	}

	@RequestMapping(value = "/beans.html", method = RequestMethod.POST)
	public String getBeans(HttpServletRequest request, String beanName, ModelMap map) {
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		String[] beanDefinitionNames = wac.getBeanDefinitionNames();
		List<String> beanNamesList = new ArrayList<String>();
		for (String string: beanDefinitionNames) {
			if (string.contains(beanName)) {
				beanNamesList.add(string);
			}
		}
		map.put("beanName", beanName);
		map.put("beanNamesList", beanNamesList);
		return "beans";
	}

	@RequestMapping(value = "/beans/{beanName}.html")
	public String getBean(HttpServletRequest request, @PathVariable("beanName") String beanName, ModelMap map) {
		return "bean";
	}

}
